﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormDeliver
    Dim objCA As New clsCPLHCK
    Dim objProdInfo As New clsProdInfo
    Dim objProdSpec As New clsProdSpec
    Dim objLhCkMes As New clsLhCkMes
    Dim dtCk As New DataTable
    Dim ids As String
    Dim mes_ids As String
    Dim prod_ids As String
    Dim type_values As String
    Dim httpHelper As New HttpHelper
    Public usr As String
    Dim warehousecode As String
    Private Sub FormDeliver_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        initDt()
        txtProdId.Focus()
    End Sub

    Public Sub initDt()
        dtCk.Columns.Add("sys_id")
        dtCk.Columns.Add("wh_no")
        dtCk.Columns.Add("prod_id")
        dtCk.Columns.Add("b_pgd_mes_sys_id")
        dtCk.Columns.Add("into_time")
        dtCk.Columns.Add("into_ope")
        dtCk.Columns.Add("type_value")
        dtCk.Columns.Add("hour_length")
    End Sub
    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            If e.KeyChar = vbCr Then
                Dim dt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim(), "0")
                If dt.Rows.Count = 1 Then
                    Dim dr As DataRow() = dtCk.Select("sys_id='" & dt.Rows(0)("sys_id") & "'")
                    If dr.Length = 0 Then
                        If dt.Rows(0)("hour_length").Equals(DBNull.Value) Then
                            dtCk.Rows.Add(dt.Rows(0)("sys_id"), dt.Rows(0)("wh_no"), dt.Rows(0)("prod_id"),
                                        dt.Rows(0)("b_pgd_mes_sys_id"), dt.Rows(0)("into_time"), dt.Rows(0)("into_ope"), dt.Rows(0)("type_value"), String.Empty)
                        Else
                            dtCk.Rows.Add(dt.Rows(0)("sys_id"), dt.Rows(0)("wh_no"), dt.Rows(0)("prod_id"),
                                        dt.Rows(0)("b_pgd_mes_sys_id"), dt.Rows(0)("into_time"), dt.Rows(0)("into_ope"), dt.Rows(0)("type_value"), dt.Rows(0)("hour_length"))
                        End If
                        ids &= "'" & dt.Rows(0)("sys_id") & "',"
                        mes_ids &= "'" & dt.Rows(0)("b_pgd_mes_sys_id") & "',"
                        prod_ids &= "'" & dt.Rows(0)("prod_id") & "',"
                        type_values &= "'" & dt.Rows(0)("type_value") & "',"
                        dgv.AutoGenerateColumns = False
                        dgv.DataSource = dtCk
                        lblRowCount.Text = dgv.Rows.Count
                        txtProdId.Text = String.Empty
                        txtProdId.Focus()
                    Else
                        MsgBox("该标签号已在表格中存在！", vbYes, "提示")
                    End If
                Else
                    If dt.Rows.Count = 0 Then
                        'MsgBox("未查询到此生产编号库存信息", vbYes, "提示")
                        Dim dtcca As DataTable = objCA.getListByProdId(txtProdId.Text.Trim())
                        If dtcca.Rows.Count = 1 Then
                            If dtcca.Rows(0)("status").ToString().Equals("1") Then
                                MsgBox("此生产编号支座已出库，无法重复出库", vbYes, "提示")
                            End If
                        End If
                    Else
                        MsgBox("查询到此生产编号有多条库存信息记录，请联系管理员", vbYes, "提示")
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub btnOUT_Click(sender As Object, e As EventArgs) Handles btnOUT.Click
        If dtCk.Rows.Count > 0 Then
            If MsgBox("请确保硫化后支座的停放时长达到设计要求的停放时长，确认出库吗？", vbYesNo, "提示") = vbYes Then
                If ids.Length > 0 Then
                    ids = ids.Substring(0, ids.Length - 1)
                End If
                If mes_ids.Length > 0 Then
                    mes_ids = mes_ids.Substring(0, mes_ids.Length - 1)
                End If
                Dim mes_id_list As String() = mes_ids.Split(",")
                If prod_ids.Length > 0 Then
                    prod_ids = prod_ids.Substring(0, prod_ids.Length - 1)
                End If
                Dim prod_id_list As String() = prod_ids.Split(",")
                If objCA.outCKs(ids, prod_ids, usr) = -2 Then
                    MsgBox("出库失败", vbYes, "提示")
                Else
                    If type_values.Length > 0 Then
                        type_values = type_values.Substring(0, type_values.Length - 1)
                    End If
                    Dim ty_v_list As String() = type_values.Split(",")
                    Dim ok_mes_ck As Integer = 0
                    For index = 0 To ty_v_list.Count() - 1
                        Dim rnt_mes_ck As String = mesCk("1", warehousecode, ty_v_list(index))
                        Dim jo As JObject = JsonConvert.DeserializeObject(rnt_mes_ck)
                        Dim code As String = jo("code").ToString() '输出0
                        Dim msg As String = jo("msg").ToString() '输出操作成功
                        Dim mes_idd As String = mes_id_list(index).Replace("'", "")
                        objLhCkMes.insert("ok", mes_idd, "1", prod_id_list(index), usr, "3", code)
                        If code.Equals("0") Then
                            ok_mes_ck = ok_mes_ck + 1
                        End If
                    Next
                    If ty_v_list.Count() = ok_mes_ck Then
                        MsgBox("出库成功", vbYes, "提示")
                    Else
                        MsgBox("出库成功，mes出库成功数：" & ok_mes_ck, vbYes, "提示")
                    End If
                    dtCk.Rows.Clear()
                    dgv.Refresh()
                    lblRowCount.Text = dgv.Rows.Count
                    ids = String.Empty
                    mes_ids = String.Empty
                    prod_ids = String.Empty
                    type_values = String.Empty
                    txtProdId.Focus()
                End If
            End If
            'Dim dtxt As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim(), "9")
            'If dtxt.Rows.Count > 0 Then
            '    Dim dtProdSpec As DataTable = objProdSpec.getProdSpecById(dtxt.Rows(0)("prod_spec_id"))
            '    Dim lh_park_time As Integer = Integer.Parse(dtProdSpec.Rows(0)("lh_park_time").ToString())
            '    Dim rtn_lh_park_time As Integer = Integer.Parse(txt_hour_length.Text.Trim()) - lh_park_time
            '    Dim transf As Integer = 0
            '    If rtn_lh_park_time >= 0 Then
            '        If objCA.outCK(sss_sys_id, txtProdId.Text.Trim(), usr) = -2 Then
            '            MsgBox("出库失败")
            '        Else
            '            MsgBox("出库成功")
            '            txtProdId.Text = String.Empty
            '            txtProdId.Focus()
            '        End If
            '    Else
            '        If MsgBox("设计硫化停放时长为： " & dtProdSpec.Rows(0)("lh_park_time").ToString() & "小时，确认出库吗？", vbYesNo, "提示") = vbYes Then
            '            If objCA.outCK(sss_sys_id, txtProdId.Text.Trim(), usr) = -2 Then
            '                MsgBox("出库失败")
            '            Else
            '                MsgBox("出库成功")
            '                txtProdId.Text = String.Empty
            '                txtProdId.Focus()
            '            End If
            '        End If
            '    End If
            'Else
            '    MsgBox("未查询到硫化后入库状态的此生产编号信息")
            'End If
        Else
            MsgBox("请扫描或输入标签号，进行出库", vbYes, "提示")
        End If
    End Sub

    Private Function mesCk(oper_num As String, warehousecode As String, productorname As String) As String
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """xhcount"":""" & Double.Parse(oper_num) & """," '出库数量
        params &= """warehousecode"":""" & warehousecode & """," '出库仓库编码
        params &= """productorname"":""" & productorname & """" '产品编码
        params &= "}"
        Return httpHelper.HttpPostWebService("materialsConsumption", params)
    End Function

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        If dgv.SelectedRows.Count > 0 Then
            dtCk.Rows.RemoveAt(dgv.CurrentRow.Index)
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtCk
            lblRowCount.Text = dgv.Rows.Count
        End If
    End Sub

    Private Sub dgv_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgv.RowPostPaint
        Dim Rectangle As Rectangle = New Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height)
        TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, Rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter Or TextFormatFlags.Right)
    End Sub
End Class